/*
	Colored Trails
	
	Copyright (C) 2006-2007, President and Fellows of Harvard College.  All Rights Reserved.
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package edu.harvard.eecs.airg.coloredtrails.shared.types;

import java.net.URL;
import java.util.ArrayList;
import java.io.Serializable;

/**
  Represents a goal
  
  WILL HAVE ARBITRARY PROPERTIES ONCE WE MOVE TO GENERAL REPRESENTATION CLASS STRUCTURE
  
  @author Sevan G. Ficici
*/
public class Goal implements Cloneable, Serializable
{
  /** default goal type */
  public static final int DEFAULT_GOAL_TYPE = 0;
  /** default goal image URL 
   * @deprecated*/
  public static URL DEFAULT_GOAL_IMAGE;

  /** the goal's ID; must be uniqe */
  private String id = "noname";
  /** the goal's type; interpretation is up to the experimenter */
  private int type = DEFAULT_GOAL_TYPE;
  /** the goal's value; interpretation is up to the experimenter */
  private double value = 0.0;
  /** the goal's location on the board */
  private RowCol location = null;
  /** whether the goal is active; interpretation is up to the experimenter */
  private boolean active = true;
  /** the goal's image URL*/
  private URL imageloc = DEFAULT_GOAL_IMAGE;
  
  /*** SPECIAL FIELDS FOR DUDI -- TO BE REMOVED FOR GENERAL CLASS **/
  /** rate at which this goal's value decays */
  private double decrement_rate = 0.0;
  /** indicates whether the goal's value jumped up or down; USE
      the constants BriefingElement.UP, BriefingElement.DOWN, and
      BriefingElement.NOCHANGE for convenience */
  private int valuejump = 0;
  
  
  /******************************************************/
  /***************** MANAGING GOAL NAMES ****************/
  /******************************************************/

  /** used to generate unique automatic goal names */
  private static int idnum = 0;
  
  private static String autoName()
  {
	  return "goal" + (idnum++);
  }
  
  
  /******************************************************/
  /***************** MANAGING GOAL IMAGES ***************/
  /******************************************************/
  
  /** list of image URLs we can index into with 'type' field 
   * @deprecated Image*/
  private static ArrayList<URL> goalimages;
  
  /**
   * buils a default list of pre-built goal images that we can index into
   */
  static
  {
	  goalimages = new ArrayList<URL>();
	  try
	  {
		  DEFAULT_GOAL_IMAGE = new URL("file://lib/images/goal.gif");
		  goalimages.add(new URL("file://lib/images/goal.gif"));
		  goalimages.add(new URL("file://lib/images/goal.gif"));
		  goalimages.add(new URL("file://lib/images/goal.gif"));
	  }
	  catch (Exception e)
	  {
		  System.out.println("Goal.static: unable to build default goal image list");
	  }
  }
  
  /**
   * Returns the specified goal image URL
   * @param index	the index into the list of goal images
   * @return		the specified URL
   * @deprecated Image
   */
  public static URL getImage(int index)
  {
	  return goalimages.get(index);
  }
  
  /**
   * clears the list of goal images that we index into
   * @deprecated Image
   */
  public static void clearImages()
  {
	  goalimages.clear();
  }
  
  /**
   * Adds an image to the end of the list of goal images we can index into
   * @param urlstring	a string containing the URL of the image
   * @deprecated Image
   */
  public static void addImage(String urlstring)
  {
	  try { goalimages.add(new URL(urlstring)); }
	  catch (Exception e) {}
  }
  
  /**
   * Sets the image list at the specified index to the specified image
   * @param urlstring	a string containing the URL of the image
   * @param index		the index into the list where we want the image placed
   * @deprecated Image
   */
  public static void setImage(String urlstring, int index)
  {
	  try { goalimages.set(index, new URL(urlstring)); }
	  catch (Exception e) {}
  }
  
  
  /******************************************************/
  /****************** INSTANCE METHODS ******************/
  /******************************************************/ 
  
  /**
    Constructor
  */
  public Goal()
  {
	  id = autoName();
  }
  
  
  public Goal(int type)
  {
	  this();
	  this.type = type;
	  imageloc = getImage(type);
  }
  
  
  public Goal(RowCol pos)
  {
	  this(DEFAULT_GOAL_TYPE, pos);
  }
  
    
  
  public Goal(String id, int type)
  {
	  this.id = id;
	  this.type = type;
	  imageloc = getImage(type);
  }
  
  
  public Goal(int type, RowCol location)
  {
	  this();
	  this.type = type;
	  this.location = location;
	  imageloc = getImage(type);
  }
  
  public Goal(String id, int type, RowCol location)
  {
	  this.id = id;
	  this.type = type;
	  this.location = location;
	  imageloc = getImage(type);
  }
  
  
  /**
    Constructor
    
    @param type     the goal's type; interpretation is up to the experimenter
    @param id       the goal's ID; interpretation is up to the experimenter
    @param imageloc the goal's image URL
    @deprecated image
  */
  public Goal(String id, int type, URL imageloc)
  {
	  this.id = id;
	  this.type = type;
	  this.imageloc = imageloc;
  }
  
  
  /**
    Copy constructor
  */
  public Goal(Goal g)
  {
    type = g.type;
    id = g.id;
    value = g.value;
    location = new RowCol(g.location);
    active = g.active;
    imageloc = g.imageloc;  // should clone URL, too? 
  }
  
  
  /**
   * Gets the Id of the Goal
   * @return Id of the goal
   */
  public String getID() { return id; }
  /**
   * Sets the Id of the Goal to id
   * @param id New Id
   */
  public void setID(String id) { this.id = id; }
  /**
   * Gets the type of the goal
   * @return Type of the goal
   */
  public int getType() { return type; }
  /**
   * Sets the type of the goal
   * @param type New type
   */
  public void setType(int type) { this.type = type; }
  /**
   * Gets the value of the goal
   * @return Value of the goal
   * @deprecated For Dudi
   */
  public double getValue() { return value; }
  /**
   * Sets the value of the goal
   * @param value
   * @deprecated For Dudi
   */
  public void setValue(double value) { this.value = value; }
  /**
   * Increments the value of the goal
   * @param delta Amount of change
   * @deprecated For Dudi
   */
  public void incrementValue(double delta) { value += delta; }
  /**
   * Decrements the value of the goal
   * @param delta Amount of change
   * @deprecated For Dudi
   */
  public void decrementValue(double delta) { value -= delta; }
  /**
   * Gets the location of the goal
   * @return Location of the goal
   */
  public RowCol getLocation() { return location; }
  /**
   * Sets the location of the goal
   * @param location New location of the goal
   */
  public void setLocation(RowCol location) { this.location = location; }
  /**
   * Sets the location of the goal
   * @param row New row value of the goal
   * @param col New colomn value of the goal
   */
  
  /*** METHODS SPECIFIC TO DUDI'S EXPERIMENT -- REMOVE FOR GENERAL CLASS ***/
  @Deprecated
  public void setDecrementRate(double dr)
  {
	  decrement_rate = dr;
  }
  @Deprecated
  public double getDecrementRate()
  {
	  return decrement_rate;
  }
  @Deprecated
  public void decrementByRate()
  {
	  decrementValue(decrement_rate);
  }
  @Deprecated
  public void setValueJump(int valuejump)
  {
	  this.valuejump = valuejump;
  }
  @Deprecated
  public int getValueJump()
  {
	  return valuejump;
  }
  
  public String toString()
  {
    StringBuffer sb = new StringBuffer();
    
    sb.append("Goal: id = " + id + "  type = " + type + "  value = " + value +
              "  location = " + location + "  URL = " + imageloc);
    
    return sb.toString();
  }
    
  
  public Object clone()
  {
    return new Goal(this);
  }
}
